﻿#pragma once

#include "Misc/EngineEditorMacros.h"
#include "Object/Object.h"
#include "PageArea/EditorPageAreaModel.h"
#include "Math/Color.h"
#include "Window/EditorContextMenuModel.h"
#include "Document/PieceTable.h"
#include "EditorPageAreaViewModel.m.h"

class REditorPageViewModel;
class RTexture2D;

RCLASS()
class REditorPageAreaViewModel : public RObject
{
    ROBJECT

public:
    void BindModel(TSharedObjectPtr<REditorPageAreaModel> InModel);
    
    const float TitleBarHeight=30; 
    const SColor PageAreaBackgroundColor = SColor::FromHexRGB(0x1F1F1F);
    int64_t GetPageNum() const;
    TSharedObjectPtr<REditorPageViewModel> GetPage(int64_t Index) const;
    TDelegate<void, TSharedObjectPtr<REditorPageViewModel>> OnPageAdded;
    TDelegate<void,int64_t> OnPageClosed;
    TDelegate<void,int64_t> OnPageReplaced;

    bool IsShowingPageContent(int64_t Index) const;
    //聚焦页面
    void SelectPage(int64_t Index);
    bool IsHighlighted(int64_t Index) const;
    TSharedObjectPtr<RTexture2D> GetPageIcon(int64_t Index) const;
    CString GetPageTitle(int64_t Index) const;

    TSharedObjectPtr<RTexture2D> GetCloseIcon(
        int64_t Index
        ,bool bIsTitleHovored
        ,bool bIsIconHovored
        ) const;


    bool HasUnsavedData(int64_t Index) const;

    void SaveFile(int64_t Index);

    void ClosePage(int64_t Index);
    
    //创建标题栏右键菜单
    TVector<SEditorContextMenuLine> CreateTitleContext(int64_t Index) const;


    //get buttons width at the right of the title
    //the buttons is both dertermined by the page area and the page
    //so different page may have different buttons
    float GetTitleOperationBarWidth();
    const float TitleOperationBarSpaceing=5;
    const float TitleOperationBarRightPadding=5;
    float GetPageOperationBarWidth();
    bool IsShowingPageOperation(int64_t Index) const;
    TSharedObjectPtr<RTexture2D> GetSplitAreaButtonIcon() ;
    void SplitArea();

    //TDelegate<void> OnLayoutChanged;
    //int64_t GetSubAreaNum() const;
    //TSharedObjectPtr<REditorPageAreaViewModel> GetSubArea(int64_t Index) const;
    //float GetSubAreaSizeWeight(int64_t Index) const;
    //bool IsSubAreaVertical() const;

protected:
    TWeakObjectPtr<REditorPageAreaModel> Model;

    TVector<TSharedObjectPtr<REditorPageViewModel>> Pages;

    TSharedObjectPtr<RTexture2D> CloseIcon;
    TSharedObjectPtr<RTexture2D> CloseIconHighlighted;
    TSharedObjectPtr<RTexture2D> UnsavedIcon;
    TSharedObjectPtr<RTexture2D> UnsavedIconHighlighted;

    TSharedObjectPtr<RTexture2D> HorizontalSpliterIcon;
    TSharedObjectPtr<RTexture2D> VerticalSpliterIcon;


    //TVector<TSharedObjectPtr<REditorPageAreaViewModel>> SubAreaViewModels;

//搜索和替换
public:
    float GetFindAndReplaceWidgetWidth() const { return FindAndReplaceWidgetWidth; }
    bool IsReplace() const ;
    void ToggleReplace();
    TSharedObjectPtr<RTexture2D> GetToggleReplaceIcon() const;

    const float LineHeight=35; //行高
    const float TextEditHeight=24; //文本框高度
    const float DragBarWidth=5;
    const float ToggleReplaceButtonWidth=20;
    const float ButtonsSize=28; //按钮大小
    const float ButtonImageSize=24; //按钮图标大小
    const float FindButtonGroupWidth=ButtonsSize*4+DragBarWidth*3; //查找按钮组宽度
    const float ButtonsPadding=5; //按钮间距
    const float ButtonGroupRightPadding=5; //按钮组右边距
    const float InputTextRightPadding=5; //输入框右边距
    float GetTextWidth() const; //文本框宽度,查找和替换共用

    void UpdateSearchText(const CString& InText);
    bool IsShowFindResultText() const { return ShowFindResultText; }
    float GetFindResultTextWidth() const;   //查找结果文本框宽度
    CString GetFindResultText() const; //查找结果文本
    SColor GetFindResultTextColor() const; //查找结果文本颜色
    bool IsFindResultEmpty() const; //是否有查找结果
    void GotoPreviousMatched();
    void GotoNextMatched();

    CString GetReplaceText() const;
    void UpdateReplaceText(const CString& InText);
    void Replace();
    void ReplaceAll();

    TSharedObjectPtr<RTexture2D> GetPreviousIcon() const;
    TSharedObjectPtr<RTexture2D> GetNextIcon() const;
    TSharedObjectPtr<RTexture2D> FindInSelectionIcon;
    TSharedObjectPtr<RTexture2D> CloseFindAndReplaceIcon;
    TSharedObjectPtr<RTexture2D> ReplaceIcon;
    TSharedObjectPtr<RTexture2D> ReplaceAllIcon;

    bool IsFindOrReplaceVisible() const;
    void CloseFindAndReplace();
protected:
    //是否显示查找替换控件
    const float MinTextWidth=100; //最小文本框宽度
    const float FindResultTextWidth=200; //查找结果文本框宽度
    float FindAndReplaceWidgetWidth=500;
    //bool bIsReplace=false; //是否显示替换
    bool ShowFindResultText=true; //是否显示查找结果文本框
    float CurrentTextWidth=200; //当前文本框宽度
    bool bIsFindInSelection=false; //是否在选中范围内查找
    //bool bIsFindOrReplace=true; //是否在查找或替换

    CString ReplaceText;

    TSharedObjectPtr<RTexture2D> CollapsedIcon;
    TSharedObjectPtr<RTexture2D> ExpandedIcon;

    TSharedObjectPtr<RTexture2D> FindPreviousIcon;
    TSharedObjectPtr<RTexture2D> FindPreviousDarkIcon;
    TSharedObjectPtr<RTexture2D> FindNextIcon;
    TSharedObjectPtr<RTexture2D> FindNextDarkIcon;

};